﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RedemptoristRetreatCenter.DatabaseUtility;
using RedemptoristRetreatCenter.HelperUtility;


namespace RedemptoristRetreatCenter
{
    /// <summary>
    /// Class for editting/deleting retreats.  
    /// </summary>
    public partial class EditRetreat : System.Web.UI.Page
    {
        /// <summary>
        /// Takes in the retreat id, title, description, duration, fee, and category id; 
        /// populates them in a ReteratInfo object, and sends that object to the DatabaseUtility
        /// to update the retreat with the sent retreat id.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void addEdittedRetreat_Click(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.QueryString.Count > 0)
                {
                    ConversionUtility conversion = new ConversionUtility();
                    populateFields(conversion.stringToInt(Request.QueryString["ID"]));
                }
            }
        }

        /// <summary>
        /// retrieve schedule data and populate controls
        /// </summary>
        /// <param name="selectedSchedule"></param>
        private void populateFields(int retreatId)
        {
            RetreatUtility retreatUtility = new RetreatUtility();
            RetreatInfo retreatInfo = retreatUtility.getRetreatById(retreatId);

            if (retreatInfo != null)
            {
                ListItem retreatItem = new ListItem(retreatInfo.title, retreatInfo.retreatId.ToString());
                ddRetreatList.SelectedIndex = ddRetreatList.Items.IndexOf(retreatItem);

                ListItem categoryItem = new ListItem(retreatInfo.categoryDesc, retreatInfo.categoryId.ToString());
                ddCategoryList.SelectedIndex = ddCategoryList.Items.IndexOf(categoryItem);

                txtTitle.Text = retreatInfo.title.ToString();
                txtDesc.Text = retreatInfo.description;
                txtDuration.Text = retreatInfo.duration.ToString();
                txtFee.Text = retreatInfo.fee.ToString();
            }
        }

        protected void save_Click(object sender, EventArgs e)
        {
            String username = getUsername();
            ConversionUtility converter = new ConversionUtility();

            RetreatInfo rInfo = new RetreatInfo();
            rInfo.retreatId = converter.stringToInt(ddRetreatList.SelectedItem.Value);
            rInfo.title = txtTitle.Text;
            rInfo.description = txtDesc.Text;
            rInfo.duration = converter.stringToInt(txtDuration.Text);
            rInfo.fee = converter.stringToInt(txtFee.Text);
            rInfo.categoryId = converter.stringToInt(ddCategoryList.SelectedItem.Value);

            RetreatUtility retreatUtility = new RetreatUtility();

            retreatUtility.openConnection();
            retreatUtility.updateRetreat(rInfo, username);
            retreatUtility.closeConnection();
                statusMessage.Text = "Retreat successfully editted.";
                statusMessage.Visible = true;
        }

        /// <summary>
        /// Deletes the retreat selected.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void delete_Click(object sender, EventArgs e)
        {
            ConversionUtility converter = new ConversionUtility();

            String username = getUsername();
            RetreatInfo rInfo = new RetreatInfo();
            rInfo.retreatId = converter.stringToInt(ddRetreatList.SelectedItem.Value);

            RetreatUtility retreatUtility = new RetreatUtility();

            retreatUtility.openConnection();
            retreatUtility.deleteRetreat(rInfo.retreatId, username);
            retreatUtility.closeConnection();
            statusMessage.Text = "Retreat successfully Deleted";
            statusMessage.Visible = true;

        }
        /// <summary>
        /// Loads the list of retreats from the database to the drop down list
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void retreatList_OnLoad(object sender, EventArgs e)
        {
            if (ddRetreatList.Items.Count == 0) // this fires everytime control is touched.  only need to load once
            {
                RetreatUtility retreatUtility = new RetreatUtility();
                List<RetreatInfo> retreats = retreatUtility.getAllRetreats();
                ddRetreatList.Items.Add(new ListItem("--Select a Retreat --", "0"));

                for (int cntr = 0; cntr < retreats.Count; cntr++)
                {
                    // add all categories (categoryDesc will be displayed and categoryId will not)
                    ddRetreatList.Items.Add(new ListItem(retreats[cntr].title, retreats[cntr].retreatId.ToString()));
                }
            }
        }
        /// <summary>
        /// Loads the list of categories from the database to the drop down list
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void categoryList_OnLoad(object sender, EventArgs e)
        {
            if (ddCategoryList.Items.Count == 0) // this fires everytime control is touched.  only need to load once
            {
                RetreatUtility retreatUtility = new RetreatUtility();
                List<RetreatCategoryInfo> retreatCategories = retreatUtility.getAllRetreatCategories();
                ddCategoryList.Items.Add(new ListItem("--Select a Category --", "0"));

                for (int cntr = 0; cntr < retreatCategories.Count; cntr++)
                {
                    // add all categories (shortDescription will be displayed and categoryId will not)
                    ddCategoryList.Items.Add(new ListItem(retreatCategories[cntr].shortDescription, retreatCategories[cntr].categoryId.ToString()));
                }
            }
        }

        // -- check to see if the retreat exists before trying to populate the text boxes --
        protected Boolean retreatExists(int id)
        {
            RetreatUtility retreatUtility = new RetreatUtility();
            RetreatInfo matchingRetreat = retreatUtility.getRetreatById(id);

            if (matchingRetreat != null)
                return true;
            else
                return false;
        }

        protected RetreatInfo getMatchingRetreat(int id)
        {
            RetreatUtility retreatUtility = new RetreatUtility();

            retreatUtility.openConnection();
            RetreatInfo matchingRetreat = retreatUtility.getRetreatById(id);
            retreatUtility.closeConnection();

            return matchingRetreat;
        }

        private string getUsername()
        {
            if (Session["Username"] != null)
            {
                return Session["Username"].ToString();
            }
            else
            {
                return String.Empty;
            }
        }
    }
}